package com.heima.kafka.controller;

import com.alibaba.fastjson.JSON;
import com.heima.kafka.pojos.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @GetMapping("send")
    public String sendMsg(){

        kafkaTemplate.send("hello-topic", "hello kafka");
        return "success";
    }

    @GetMapping("sendObjecgt")
    public String sendObjecgt(){
        User user = new User();
        user.setUsername("xiaowang");
        user.setAge(18);


        kafkaTemplate.send("hello-topic-object", JSON.toJSONString(user));
        return "success";
    }
}
